package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.web.core.base.BaseController;
import com.ruoyi.work.domain.Gzd;
import com.ruoyi.work.service.IGzdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 工作台应用管理 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-10-24
 */
@Controller
@RequestMapping("/work/gzd")
public class GzdController extends BaseController
{
    private String prefix = "work/gzd";
	
	@Autowired
	private IGzdService gzdService;
	
	@RequiresPermissions("work:gzd:view")
	@GetMapping()
	public String gzd()
	{
	    return prefix + "/gzd";
	}
	
	/**
	 * 查询工作台应用管理列表
	 */
	@RequiresPermissions("work:gzd:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Gzd gzd)
	{
		startPage();
        List<Gzd> list = gzdService.selectGzdList(gzd);
		for (Gzd gz : list) {
			gz.setYyImg(Constants.IMG_URL + gz.getYyImg());
		}
		return getDataTable(list);
	}
	
	/**
	 * 新增工作台应用管理
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存工作台应用管理
	 *   上传图片
	 */
	@RequiresPermissions("work:gzd:add")
	@Log(title = "工作台应用管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public  Map<String, Object> addSave(Gzd gzd, HttpServletRequest request, HttpServletResponse response)
	{

		// ResourceBundle bundle = PropertyResourceBundle.getBundle("application");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		//  String rootPath = bundle.getString("upLoadUrl");
		String filePath = "D:/";
		Map<String, Object> map = new HashMap<>();
		map = uploadFiles(filePath, fileMap);
		return map;
	}

	public  Map<String, Object> uploadFiles(String savePath, Map<String,MultipartFile> fiLeMap){
		Map<String, Object> map = new HashMap<>();
		try {
			String fileName = "";
			if(fiLeMap!=null){
				for(Map.Entry<String, MultipartFile> entity:fiLeMap.entrySet()){
					MultipartFile f = entity.getValue();
					if(f != null && !f.isEmpty()){
						String uuid = UUID.randomUUID().toString();
						fileName = uuid + "#" + f.getOriginalFilename();
						File newFile = new File(savePath + "/" + fileName);
						f.transferTo(newFile);
					}
				}
			}
			map.put("success", true);
			map.put("fileName", fileName);
			return map;
		}catch (Exception e) {
			map.put("success", false);
			return map;
		}
	}

	/**
	 * 修改工作台应用管理
	 */
	@GetMapping("/edit/{gztId}")
	public String edit(@PathVariable("gztId") Integer gztId, ModelMap mmap)
	{
		Gzd gzd = gzdService.selectGzdById(gztId);
		mmap.put("gzd", gzd);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存工作台应用管理
	 */
	@RequiresPermissions("work:gzd:edit")
	@Log(title = "工作台应用管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Gzd gzd)
	{		
		return toAjax(gzdService.updateGzd(gzd));
	}
	
	/**
	 * 删除工作台应用管理
	 */
	@RequiresPermissions("work:gzd:remove")
	@Log(title = "工作台应用管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(gzdService.deleteGzdByIds(ids));
	}
	
}
